package com.ruyuan.eshop.diplomacy.manager.custom.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import com.ruyuan.eshop.diplomacy.domain.entity.CategoryInfoDO;
import com.ruyuan.eshop.common.enums.DelFlagEnum;
import com.ruyuan.eshop.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import com.ruyuan.eshop.diplomacy.manager.common.cache.RedisStringCache;
import com.ruyuan.eshop.diplomacy.mapper.CategoryBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * 商品品类信息
 *
 * @author zhonghuashishan
 */
@Service("categoryBaseStringDatabase")
public class CategoryBaseStringDatabase extends AbstractRedisStringDatabase<CategoryInfoDO> {

    @Resource
    private CategoryBaseMapper categoryBaseMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "CATEGORY_INFO";

    /**
     * 品类ID
     */
    public static final String CATEGORY_ID = "categoryId";

    /**
     * 数据源单表
     *
     * @return java.lang.String
     * @author zhonghuashishan
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 数据源全量表
     *
     * @param
     * @return java.util.Set<java.lang.String>
     * @author zhonghuashishan
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(getTableName());
    }

    /**
     * 获取表数据
     *
     * @param tableFieldsMap    key 数据库字段，value 字段值
     * @param queryType         查询类型，多数据查询时区分
     * @return java.util.Optional<com.ruyuan.eshop.diplomacy.domain.entity.CategoryInfoDO>
     * @author zhonghuashishan
     */
    @Override
    public Optional<CategoryInfoDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (tableFieldsMap.containsKey(CATEGORY_ID)) {
            QueryWrapper<CategoryInfoDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("ID", Sets.newHashSet((Integer) tableFieldsMap.get(CATEGORY_ID)));
            List<CategoryInfoDO> categoryInfoDOList = categoryBaseMapper.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(categoryInfoDOList)) {
                /*过滤删除的*/
                CategoryInfoDO doBase = categoryInfoDOList.get(0);
                if (doBase.getDelFlag() == 1) {
                    return Optional.of(doBase);
                }
            }
            return Optional.empty();
        }
        throw new UnsupportedOperationException();
    }

    /**
     * 获取表集合数据
     *
     * @param tableFieldsMap
     * @param queryType
     * @return java.util.Optional<java.util.List < T>>
     * @author zhonghuashishan
     */
    @Override
    public Optional<List<CategoryInfoDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        return Optional.empty();
    }

    /**
     * 根据ID批量获取数据
     *
     * @param idSet         表主键ID集合
     * @param queryType     查询类型，多数据查询时区分
     * @return java.util.Optional<com.ruyuan.eshop.diplomacy.manager.common.source.RedisStringSource<com.ruyuan.eshop.diplomacy.domain.entity.CategoryInfoDO>>
     * @author zhonghuashishan
     */
    @Override
    public Optional<RedisStringCache<CategoryInfoDO>> listTableDataByIdSet(Set<Integer> idSet, String queryType) {
        QueryWrapper<CategoryInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("ID", idSet);
        List<CategoryInfoDO> materialCategoryDOList = categoryBaseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(materialCategoryDOList)) {
            return Optional.empty();
        }
        RedisStringCache redisStringCache = new RedisStringCache();
        List<CategoryInfoDO> addList = materialCategoryDOList.stream().filter(doBase -> doBase.getDelFlag() == DelFlagEnum.EFFECTIVE.getCode()).collect(toList());
        Set<String> wait2deleteSet = materialCategoryDOList.stream().filter(doBase -> doBase.getDelFlag() == DelFlagEnum.DISABLED.getCode()).map(doBase -> String.valueOf(doBase.getId())).collect(toSet());
        redisStringCache.setAddList(addList);
        redisStringCache.setDeleteSet(wait2deleteSet);
        return Optional.of(redisStringCache);
    }

    @Override
    public Optional<RedisStringCache<CategoryInfoDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        return Optional.empty();
    }

}
